﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Net.Mime.MediaTypeNames;
using System.Windows;
using System.IO;
using System.Windows.Controls;

namespace CSharpToolWPF.Classes
{
    public class FilePathTextBox : TextBox {
        bool draggingOnlyOneFile = true;
        List<string> dragFileList = new List<string>();
        public override void OnApplyTemplate() {
            base.OnApplyTemplate();

            Style = (Style)FindResource(typeof(TextBox));

            DragEnter += FilePathTextBox_DragEnter;
            Drop += FilePathTextBox_Drop;
            PreviewDragOver += FilePathTextBox_PreviewDragOver;
        }

        private void FilePathTextBox_PreviewDragOver(object sender, DragEventArgs e) {
            e.Handled = true;
        }

        private void FilePathTextBox_DragEnter(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                e.Effects = DragDropEffects.Copy;
            }
            else
                e.Effects = DragDropEffects.None;
            e.Handled = true;
        }

        private void FilePathTextBox_Drop(object sender, DragEventArgs e) {
            if (e.Data.GetDataPresent(DataFormats.FileDrop)) {
                // 获取文件路径数组
                string[] files = (string[])e.Data.GetData(DataFormats.FileDrop);
                if (files.Length > 1) {
                    this.Text = "不要拖入多个文件！";
                    return;
                }
                this.Text = files[0];
                return;
            }
        }
    }
}
